#include "SFML/Graphics.hpp"
#include "gameGlobals.h"
#include "tchar.h"

using namespace sf;

class Game
{
private:
	IPAddress ipServer;
	//SocketTCP TsocksClient;
	SocketUDP UsocksClient;
	unsigned int connectionPort;
	unsigned int clientID;
	bool SendData(char[]);
public:
	Game();
	RenderWindow rwApp;
	bool ShowSplash (void);
	bool DoWindowEvents (void);
	bool ProccessSplash(void);
	bool isRunning;
	bool MouseInRegion(unsigned int,unsigned int,unsigned int,unsigned int);
	Image imgSplash;
	Sprite sprSplash;
	bool isStarted;
	String sfStrIP;
	std::string strIP;
	bool Connect();	
};

Game::Game()
{
	rwApp.Create(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT),"Pong");
	connectionPort = 1337;
	isRunning = true;
	isStarted = false;
}

bool Game::SendData(char toSend[])
{
	if (UsocksClient.Send(toSend,sizeof(toSend),ipServer,connectionPort)!=Socket::Done)
		return false;
	return true;
}
bool Game::Connect()
{
	ipServer = IPAddress(strIP);
	if (!ipServer.IsValid())
	{
		MsgBox("Error","Invalid IP Address-"+ipServer.ToString());
		return false;
	}
	UsocksClient.Bind(connectionPort);
	SendData("OPEN");

	//MsgBox("OK","Connecting to "+strIP);
	return true;

}

bool Game::MouseInRegion(unsigned int lowX,unsigned int highX,unsigned int lowY,unsigned int highY)
{
	if (rwApp.GetInput().GetMouseX()<lowX)
		return false;
	if (rwApp.GetInput().GetMouseX()>highX)
		return false;
	if (rwApp.GetInput().GetMouseY()<lowY)
		return false;
	if (rwApp.GetInput().GetMouseY()>highY)
		return false;
	return true;
}
bool Game::ShowSplash()
{
	sfStrIP.Move(345.f,127.f);
	sfStrIP.SetFont("arial.ttf");
	sfStrIP.SetSize(14.f);
	sfStrIP.SetColor(Color(255,255,255));
	if (!imgSplash.LoadFromFile("images/splash.bmp"))
		return false;
	sprSplash.SetImage(imgSplash);
	rwApp.Draw(sprSplash);
	rwApp.Display();
	return true;
}
bool Game::ProccessSplash()
{
	Event evt;
	while (rwApp.GetEvent(evt))
	{
		if (evt.Type==Event::Closed)		
			isRunning = false;		
		if ((evt.Type == Event::KeyPressed) && (evt.Key.Code == Key::Escape))
			isRunning = false;
		if ((evt.Type == Event::KeyPressed) && (evt.Key.Code == Key::Back))
		{
			strIP.erase(strIP.end()-1);
			sfStrIP.SetText(strIP);
		}
		if (evt.Type == Event::TextEntered && (strIP.length()<15))
		{
			if ((char)evt.Text.Unicode!=8)
			{
				strIP+=(char)evt.Text.Unicode;
				sfStrIP.SetText(strIP);		
			}
		}
		if (evt.Type == Event::MouseButtonPressed)
		{
			if (evt.MouseButton.Button==Mouse::Left)
			{
				if (MouseInRegion(332,452,200,225))
				{
					if (Game::Connect())
						isStarted=true;
				}
				if (MouseInRegion(332,452,245,270))
				{
					isRunning=false;
				}
			}
		}
	}	
	rwApp.Draw(sprSplash);
	rwApp.Draw(sfStrIP);
	rwApp.Display();
	return true;
}
bool Game::DoWindowEvents()
{
	Event evt;
	while (rwApp.GetEvent(evt))
	{
		if (evt.Type==Event::Closed)		
			isRunning = false;		
		if ((evt.Type == Event::KeyPressed) && (evt.Key.Code == Key::Escape))
			isRunning = false;
	}
	return true;
}

